import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

import { ModalService } from '../shared/modal/modal.service';
import { ConfirmConfig } from '../shared/modal/modal-model';

import { AvatarCropperComponent } from '../business-shared/user/avatar-cropper.component';
import { PasswordEditComponent } from '../business-shared/user/password-edit.component';
import { AppService } from '../app.service';

import { PassportService } from '../services/passport.service';
import { SysService } from '../services/sys.service';

import {ProfileModel} from '../models/ProfileModel';
import {AppModel} from '../models/AppModel';
import {MenuModel} from '../models/MenuModel';

/**
* 主体组件
*/
@Component({
    selector: 'app-main',
    templateUrl: './main.component.html'
})
export class MainComponent implements OnInit {

    // 切换导航
    toggleDescTip = '点击关闭导航菜单';

    // 切换导航标识
    navClose = false;

    profileInfo: ProfileModel = null;
    appInfo: AppModel = null;
    menus: Array<MenuModel> = null;

    title = '首页';


    constructor(
        private router: Router,
        private modalService: ModalService,
        private ngbModalService: NgbModal,
        private appService: AppService,
        private passportService: PassportService,
        private sysService: SysService
    ) {
        const that = this;
        this.appService.titleEventEmitter.subscribe((value: string) => {
            if (value) {
                that.title = value;
            }
        });

        this.profileInfo = this.passportService.getProfile();

        this.sysService.getMenus((successful, menuDatas) => {
            if (successful) {
                that.menus = menuDatas;
            }
        });
    }


    /**
    * 初始化
    */
    ngOnInit() {
        if ( !this.passportService.isAuthenticated()) {
            this.router.navigate(['/login']);
        }
    }

    /**
    * 切换导航
    */
    toggleNav() {
        this.navClose = !this.navClose;
        if (this.navClose) {
            this.toggleDescTip = '点击展开导航菜单';
        } else {
            this.toggleDescTip = '点击关闭导航菜单';
        }
    }

    /**
    * 跳转首页
    */
    toHome() {
        this.title = '首页';
        this.router.navigate(['/app/home']);
    }

    /**
    * 个人资料
    */
    userInfo() {
        this.router.navigate(['/app/user/userInfo']);
    }

    /**
    * 头像更换
    */
    avatarReplacement() {
        this.ngbModalService.open(AvatarCropperComponent, { size: 'lg', backdrop: 'static', keyboard: false }).result.then((result) => {

        }, (reason) => {

        });
    }

    /**
    * 修改密码
    */
    passwordEdit() {
        this.ngbModalService.open(PasswordEditComponent, { size: 'lg' }).result.then((result) => {

        }, (reason) => {

        });
    }


    /**
    * 退出系统
    */
    exitSys() {
        const exitSysCfg = new ConfirmConfig('您确定退出系统吗？');
        this.modalService.confirm(exitSysCfg).then((result) => {
            if (result.status === 'approved') {
                this.router.navigate(['/login']);
            }
        }, (reason) => {
        });
    }





}
